package com.house.controller;

import cn.hutool.core.bean.BeanUtil;
import com.house.dto.MessageDTO;
import com.house.entity.Bill;
import com.house.entity.Message;
import com.house.service.MessageService;
import com.house.utils.Code;
import com.house.utils.Result;
import com.house.vo.BillVO;
import com.house.vo.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: GGP
 * @description: 留言控制类
 * @time: 2023-04-17 21:50
 **/
@Api("留言")
@RestController
@RequestMapping("/message")
@Validated
public class MessageController {

	@Resource
	private MessageService messageService;

	/**
	 * 查看所有留言
	 * @param messageDTO
	 * @return
	 */
	@ApiOperation(value = "查看所有留言")
	@PostMapping("/findAll")
	public Result findAll(@RequestBody MessageDTO messageDTO){
		Map<String,Object> map=new HashMap<>(3);
		List<Map<String,Object>> allMessage = messageService.findAllMessage(messageDTO);
		List<MessageVO> messagesVO = BeanUtil.copyToList(allMessage, MessageVO.class);
		map.put("messages",messagesVO);
		return new Result(Code.GET_OK,map,"查询成功");
	}

	/**
	 * 添加留言
	 * @param messageDTO
	 * @return
	 */
	@ApiOperation(value = "添加留言")
	@PostMapping("/save")
	public Result saveMessage(@RequestBody MessageDTO messageDTO){
		messageService.saveMessage(messageDTO);
		return new Result(Code.SAVE_OK,"添加成功");
	}
}
